// pages/saoma/saoma.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    SCanCode: "",
    danhao:"",
    userId: app.globalData.userId,
    chukuInfo:[],
    chukuInfoWei:[],
    ReChukuInfo:[],
    ReNum:false,
    ReValue: 0,
    danid:0,
  },
  saoma:function () {
    var that = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success (res) {
        console.log(res)
        that.setData({
          SCanCode: res.result
        })
        var checkCode = wx.request({
          url: 'https://127.0.0.1:10270/api/saoMa2XiaoChengXu?userId='+that.data.userId
          +"&code="+that.data.SCanCode
          +"&danhao="+that.data.danhao
          +"&danid="+that.data.danid
          +"&remark="+that.data.chukuInfoWei["remark"],
          method: 'POST',
          success: (result) => {
            if (result.data == "已成功出库") {
              wx.showModal({
                title: '出库结果',
                content: '商品出库成功',
                complete: (res) => {
                  console.log("res");
                  // wx.navigateTo({ 
                  //   url: '/pages/saoma/saoma?danhao='+that.data.danhao, 
                  // }) 
                  that.onLoad({
                    danhao: that.data.danhao,
                    id:that.data.danid
                  })
                }
              })
            }
            else if (result.data == "整单出库完成") {
              wx.showModal({
                title: '出库结果',
                content: '商品出库成功，本单所需商品已全部出库',
                complete: (res) => {
                  that.onLoad({
                    danhao: that.data.danhao,
                    id:that.data.danid
                  })
                }
              })
            }
            else{
              wx.showModal({
                title: '出库结果',
                content: result.data,
              })
            }
          },
          fail: (err) => {
            wx.showModal({
              title: err.data,
            })
          },
          complete: (res) => {
            checkCode.abort()
          },
        })
      },
      fail (err) {
        console.log(err.errMsg)
        wx.showToast({
          title:'扫描失败',
          icon: 'none',
          duration: 1000
        })},
    })
  },
  itemReSetNum:function (params) {
    var that = this
    var chukuInfo = params.currentTarget.dataset.item
    this.setData({
      ReChukuInfo: chukuInfo,
      ReNum: true,
      ReValue: chukuInfo.sahngpinshuliang
    })
  },
  ReNumCandel: function () {
    // do something
    this.setData({
      ReNum:false,
      ReChukuInfo: [],
      ReValue: 0
    })
  },

  /**
    *  点击确认
    */
   ReNumConfirm: function () {
    // do something
    this.setData({
      ReNum:false
    })
    var that = this
    var req = wx.request({
      url: 'https://127.0.0.1:10270/api/itemReSetNum?fahuodanhao='+that.data.danhao
            +'&shangpinBianhao='+that.data.ReChukuInfo.shangpinbianhao
            +'&shangpinShuliang='+that.data.ReValue
            +'&danid='+that.data.danid
            +'&userid='+that.data.userId,
      method: 'post',
      success: (result) => {
        // console.log(result)
        if (result.data == '数量修改成功') {
          wx.showToast({
            title: result.data,
            icon: "success",
            duration:1500
          })
        }else{
          wx.showModal({
            content: result.data,
            icon: "error",
          })
        }
        req.abort()
      },
      fail: (err) => {},
      complete: (res) => {
        that.onLoad({
          danhao: that.data.danhao,
          id:that.data.danid
        })
      },
    })
  },
  valuechange: function (params) {
    this.setData({
      ReValue: params.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var uid = app.globalData.userId
    this.setData({ 
      danhao:options.danhao,
      danid:options.id,
      chukuInfoWei: app.globalData.chuku,
      userId: uid
    })
    var that = this
    var dataget = wx.request({
      url: 'https://127.0.0.1:10270/api/chukuDanList?danhao=' + options.danhao+'&danid='+options.id,
      method: "post",
      success: (result) => {
        console.log(result.data);
        that.setData({
          chukuInfo: result.data
        })
       dataget.abort()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})